<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('政策台账列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label style="display:inline-block;white-space:nowrap;width: auto;">发布文号：</label>
                                <input type="text" name="code"/>
                            </li>
                            <li>
                                <label style="display:inline-block;white-space:nowrap;width: auto;">政策名称：</label>
                                <input type="text" name="name"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:policy:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="system:policy:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="system:policy:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:policy:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('system:policy:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:policy:remove')}]];
        var prefix = ctx + "system/policy";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "政策台账",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'type',
                    title: '政策类别'
                },
                {
                    field: 'code',
                    title: '发布文号'
                },
                {
                    field: 'name',
                    title: '政策名称'
                },
                {
                    field: 'pubUnit',
                    title: '发布单位'
                },
                {
                    field: 'pubDate',
                    title: '发布日期'
                },
                {
                    field: 'implDate',
                    title: '实施日期'
                },
                {
                    field: 'project',
                    title: '服务分类'
                },
                {
                    field: 'done',
                    title: '是否完成收集'
                },
                {
                    field: 'collecter',
                    title: '收集人'
                },
                {
                    field: 'collecterTime',
                    title: '收集时间'
                },
                    {
                        field : 'filePath',
                        title: '文件预览',
                        formatter: function(value, row, index) {
                            if(row.filePath) {
                                let regex = /[^/]+(?!.*\/)/
                                let filePathAll = row.filePath;
                                let search = filePathAll.match(regex)[0];
                                console.log(search)
                                let fileArr = search.split('\.')
                                let suffix = fileArr[1]
                                let fileNameArr = fileArr[0].split('_');
                                let filePath = fileNameArr[0] + "." + suffix

                                // return '<a class="filePath" target="_blank" href="' + row.filePath + '">'+ filePath + '</a>';
                                return '<a class="filePath" target="_blank" href="' + row.filePath + '">附件</a>';
                            } else {
                                return '<input class="filePath" type="file" name="filePath" />';
                            }
                            // return '<a target="_blank" href="' + row.filePath + '">'+ row.fileName + '</a>';
                            // return '<a href="javascript:downloadFile(' + row.fileId + ')"><img style="width:30;height:30px;"  src="/profile/upload/' + row.filePath + '"/></a>';
                        }
                    },
                // {
                //     field: 'remarks',
                //     title: '备注'
                // },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(!row.filePath) {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="uploadFile(' + JSON.stringify(row).replace(/\"/g, "'") + ',\'' + row.id + '\', ' + index +')"><i class="fa fa-edit"></i>上传</a> ')
                        } else {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="clearFile(\'' + row.id + '\')"><i class="fa fa-edit"></i>重传</a> ')
                        }
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });


        function uploadFile(row, id, index) {
            console.log(row.id, row.filePath)
            // console.log(rowStr)
            // let row = JSON.parse(rowStr)
            // console.log("============================")
            // console.log(row)
            var formData = new FormData();
            let filePath =$('.filePath')[index];

            if(filePath.files[0] == null) {
                $.modal.alertWarning("请先选择文件路径!")
                return false;
            }
            formData.set('file', filePath.files[0]);
            formData.set('id', id);

            $.ajax({
                url: prefix + "/upload",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }

        function clearFile(id) {
            $.ajax({
                // url: prefix + "/clearFile?id="+id,
                url: prefix + "/clearFile",
                type: 'post',
                cache: false,
                data: {"id": id},
                // data: null,
                // processData: false,
                // contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }
    </script>
</body>
</html>